package com.jcl.recruit.user.dao;

import com.jcl.recruit.core.common.page.AbstractEntityPage;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.core.db.DatabaseContext;
import com.jcl.recruit.user.mapper.UserMapper;
import com.jcl.recruit.user.model.User;
import com.jcl.recruit.user.vo.UserSearchEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;


@Component
public class CorpUserDao extends AbstractEntityPage<UserSearchEntity> implements UserMapper {

    @Autowired
    private UserMapper userMapper;


    @Override
    protected List<?> searchConditionList(UserSearchEntity entity) {
        DatabaseContext.setCurrentContext();
        if (UserSearchEntity.TYPE_CERTIFY.equals(entity.getType())) {
            entity.setStatus(Constants.UserInfo.USER_STATUS_UNACTIVE);
            return userMapper.findListByEntity(entity);
        } else if (UserSearchEntity.TYPE_ALL.equals(entity.getType())) {
            return userMapper.findListByEntity(entity);
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> selectUsersByCondition(String realname,
                                                            Integer isHr,
                                                            String corpCode,
                                                            Integer departId) {
        DatabaseContext.setContext(corpCode);
        List<Map<String, Object>> userVos = userMapper.selectUsersByCondition(realname, isHr, corpCode, departId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map<String, Object> entity : userVos) {
            entity.put("createtime", entity.get("createtime"));
        }
        return userVos;
    }

    @Override
    public List<User> findListByEntity(UserSearchEntity entity) {
        DatabaseContext.setCurrentContext();
        return userMapper.findListByEntity(entity);
    }

    @Override
    public List<User> findAll(String corpCode) {
        DatabaseContext.setContext(corpCode);
        return userMapper.findAll(corpCode);
    }

    public List<User> findListCertification(String corpCode) {
        DatabaseContext.setContext(corpCode);
        return userMapper.findListByStatus(corpCode, Constants.UserInfo.USER_STATUS_UNACTIVE);
    }

    @Override
    public List<User> findUserListByIds(String corpCode, List<Integer> ids) {
        DatabaseContext.setContext(corpCode);
        return userMapper.findUserListByIds(corpCode, ids);
    }

    @Override
    public List<User> findUserList(List<Integer> ids) {
        DatabaseContext.setCurrentContext();
        return userMapper.findUserList(ids);
    }

    @Override
    public List<User> findListByStatus(String corpCode, Integer status) {
        DatabaseContext.setContext(corpCode);
        return userMapper.findListByStatus(corpCode, status);
    }

    @Override
    public List<User> findAdminUserListByCorp(List<String> corpCodes) {
        DatabaseContext.setCurrentContext();
        return userMapper.findAdminUserListByCorp(corpCodes);
    }

    @Override
    public Map<String, Object> selectPrimaryKey(String username, String password) {
        DatabaseContext.setCurrentContext();
        return userMapper.selectPrimaryKey(username, password);
    }

    @Override
    public Map<String, Object> selectMaxIdKey() {
        DatabaseContext.setCurrentContext();
        return userMapper.selectMaxIdKey();
    }

    @Override
    public Map<String, Object> countMember(String corpCode) {
        DatabaseContext.setContext(corpCode);
        return userMapper.countMember(corpCode);
    }

    @Override
    public Map<String, Object> selectPrimaryKeyByToken(String token) {
        DatabaseContext.setCurrentContext();
        return userMapper.selectPrimaryKeyByToken(token);
    }

    @Override
    public Map<String, Object> selectPrimaryKeyById(Integer id) {
        DatabaseContext.setCurrentContext();
        return userMapper.selectPrimaryKeyById(id);
    }

    @Override
    public Map<String, Object> selectPrimayByUsername(String username) {
        DatabaseContext.setCurrentContext();
        return userMapper.selectPrimayByUsername(username);
    }

    @Override
    public User findOneById(Integer id) {
        DatabaseContext.setCurrentContext();
        return userMapper.findOneById(id);
    }

    @Override
    public User findOne(String corpCode, Integer id) {
        DatabaseContext.setContext(corpCode);
        return userMapper.findOne(corpCode, id);
    }

    @Override
    public User findAdminUserByCorp(String corpCode) {
        DatabaseContext.setContext(corpCode);
        return userMapper.findAdminUserByCorp(corpCode);
    }

    @Override
    public int insertSelective(User entity) {
        DatabaseContext.setContext(entity.getCorpCode());
        return userMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(User entity) {
        DatabaseContext.setContext(entity.getCorpCode());
        return userMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public int updateUserHead(Integer id, String photoId) {
        DatabaseContext.setCurrentContext();
        return userMapper.updateUserHead(id, photoId);
    }

    @Override
    public Integer selectPrimaryIdByEmail(String email) {
        DatabaseContext.setCurrentContext();
        return userMapper.selectPrimaryIdByEmail(email);
    }
}
